package com.gridmobi.xrtb.domain.dsp.common;

import com.google.common.collect.Maps;
import com.gridmobi.xrtb.domain.dsp.common.enums.CfgKeyE;
import com.gridmobi.xrtb.domain.dsp.common.enums.CfgTypeE;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author huangyongchao
 */
public class Tools {


    public static String idfaConvKey(String appid){
        return AppConstant.CONV_PREFIX.concat(appid);
    }
    /**
     * 分页相关
     */
    public static Integer DEFAULT_PAGE = 1;
    public static Integer DEFAULT_PAGE_SIZE = 10;

    public static void adjustPage(Integer page, Integer pageSize) {
        if (page == null || page < 1) {
            page = DEFAULT_PAGE;
        }
        if (pageSize == null || pageSize < 1 || pageSize > 2000) {
            pageSize = DEFAULT_PAGE_SIZE;
        }
    }

    public static String listToStr(List list) {
        if (CollectionUtils.isEmpty(list)) {
            return "";
        } else {
            StringBuffer s = new StringBuffer();
            list.forEach(n -> s.append(n).append(","));
            return s.toString().substring(0, s.length() - 1);
        }
    }

    public static void main(String[] args) {
        System.out.println(listToStr(Arrays.asList("aaaa", "bbbb", "cccc ")));
    }

    public static String  reportGroupKey(int group){
        String cfgkey = null;
        switch (group) {
            case 1:
                cfgkey = CfgKeyE.INVENTORY_APPBUNDLES.key;
                break;
            case 2:
                cfgkey = CfgKeyE.INVENTORY_PUBLISHERS.key;
                break;
            case 8:
                cfgkey = CfgKeyE.TARGETING_CARRIERS.key;
                break;
            case 9:
                cfgkey = CfgKeyE.TARGETING_IPRANGES.key;
                break;
        }
        return cfgkey;

    }

    public static void cfgvDefault(Map j){
        if(!j.containsKey(CfgKeyE.INVENTORY_APPBUNDLES.key)){
            Map m = Maps.newHashMap();
            m.put(CfgTypeE.INCLUDE.code, AppConstant.DEFAULT_CFGV);
            j.put(CfgKeyE.INVENTORY_APPBUNDLES.key, m);
        }
        if(!j.containsKey(CfgKeyE.TARGETING_COUNTRIES.key)){
            Map m = Maps.newHashMap();
            m.put(CfgTypeE.INCLUDE.code, AppConstant.DEFAULT_CFGV);
            j.put(CfgKeyE.TARGETING_COUNTRIES.key, m);
        }
        if(!j.containsKey(CfgKeyE.TARGETING_CARRIERS.key)){
            Map m = Maps.newHashMap();
            m.put(CfgTypeE.INCLUDE.code, AppConstant.DEFAULT_CFGV);
            j.put(CfgKeyE.TARGETING_CARRIERS.key, m);
        }
        if(!j.containsKey(CfgKeyE.INVENTORY_PUBLISHERS.key)){
            Map m = Maps.newHashMap();
            m.put(CfgTypeE.INCLUDE.code, AppConstant.DEFAULT_CFGV);
            j.put(CfgKeyE.INVENTORY_PUBLISHERS.key, m);
        }
        if(!j.containsKey(CfgKeyE.TARGETING_IPRANGES.key)){
            Map m = Maps.newHashMap();
            m.put(CfgTypeE.INCLUDE.code, AppConstant.DEFAULT_CFGV);
            j.put(CfgKeyE.TARGETING_IPRANGES.key, m);
        }
        if(!j.containsKey(CfgKeyE.TARGETING_DEVICEMODEL.key)){
            Map m = Maps.newHashMap();
            m.put(CfgTypeE.INCLUDE.code, AppConstant.DEFAULT_CFGV);
            j.put(CfgKeyE.TARGETING_DEVICEMODEL.key, m);
        }
        if(!j.containsKey(CfgKeyE.TARGETING_DEVICEID.key)){
            Map m = Maps.newHashMap();
            m.put(CfgTypeE.INCLUDE.code, AppConstant.DEFAULT_CFGV);
            j.put(CfgKeyE.TARGETING_DEVICEID.key, m);
        }
        if(!j.containsKey(CfgKeyE.INVENTORY_DOMAINLIST.key)){
            Map m = Maps.newHashMap();
            m.put(CfgTypeE.INCLUDE.code, AppConstant.DEFAULT_CFGV);
            j.put(CfgKeyE.INVENTORY_DOMAINLIST.key, m);
        }
    }
    public static String landurlRedisKey(String reqid) {
        return AppConstant.LANDURLPREFIX.concat(reqid);
    }


    /**
     * next 。。
     */
}
